﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureMRCore.DataModel
{
    public class ReduceInputDataModel : TableServiceEntity
    {
        public ReduceInputDataModel()
        {
            base.RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
            FetchedBy = "";
        }

        public ReduceInputDataModel(string jobID, int reduceID, string uri, string iteration, string mapID)
            : this()
        {
            JobID = jobID;
            ReduceID = reduceID;
            URI = uri;
            Key = "";
            var strings = mapID.Split('_');
            int mapIndex = Int32.Parse(strings[0]);
            //base.RowKey = iteration;
            base.PartitionKey = iteration.PadLeft(5, '0') + "__" + (mapIndex%16);
        }

        public string JobID { get; set; }
        public int ReduceID { get; set; }
        public string MapID { get; set; }

        public string Key { get; set; }
        public byte[] Value { get; set; }
        public int ValueCount { get; set; }
        public string URI { get; set; }

        public string StartTime { get; set; }
        public string FetchedBy { get; set; }
        public string PutTime { get; set; }
        public int Attempt { get; set; }
        public string FetchStart { get; set; }
        public long FetchTime { get; set; }
    }

    public class ReduceRecordComparer : IEqualityComparer<ReduceInputDataModel>
    {
        // Products are equal if their names and product numbers are equal.

        #region IEqualityComparer<ReduceInputDataModel> Members

        public bool Equals(ReduceInputDataModel x, ReduceInputDataModel y)
        {
            //Check whether the compared objects reference the same data.
            if (ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.RowKey == y.RowKey;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(ReduceInputDataModel record)
        {
            //Check whether the object is null
            if (ReferenceEquals(record, null)) return 0;

            //Get hash code for the Name field if it is not null.
            int hashRowKey = record.RowKey == null ? 0 : record.RowKey.GetHashCode();

            return hashRowKey;
        }

        #endregion
    }
}